package com.nineclock.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.attendance.dto.DepartmentOrUserSimpleDTO;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.common.utils.CurrentUserHolder;
import com.nineclock.system.dto.SysDepartmentDTO;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.mapper.SysDepartmentMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.pojo.SysDepartment;
import com.nineclock.system.service.SysDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysDepartmentServiceImpl implements SysDepartmentService {

    @Autowired
    private SysDepartmentMapper departmentMapper;
    @Autowired
    SysCompanyUserMapper companyUserMapper;

    @Override
    public List<SysDepartmentDTO> queryDepartmentList() {
        return this.queryChildDepartmentList(0L);
    }

    public List<SysDepartmentDTO> queryChildDepartmentList(Long parenId){
        LambdaQueryWrapper<SysDepartment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());
        queryWrapper.eq(SysDepartment::getParentId,parenId);
        List<SysDepartment> sysDepartments = departmentMapper.selectList(queryWrapper);

        if(CollectionUtil.isNotEmpty(sysDepartments)) {
            List<SysDepartmentDTO> departmentDTOList = BeanHelper.copyWithCollection(sysDepartments, SysDepartmentDTO.class);
            for (SysDepartmentDTO departmentDTO : departmentDTOList) {
                //查询子部门列表信息
                List<SysDepartmentDTO> childDepartmentDTOList = this.queryChildDepartmentList(departmentDTO.getId());
                departmentDTO.setChildren(childDepartmentDTOList);
            }
            return departmentDTOList;
        }
        return new ArrayList<>();
    }

    @Override
    public List<Long> queryDepartmentIds() {
        LambdaQueryWrapper<SysDepartment> departmentQueryWrapper = new LambdaQueryWrapper<>();
        departmentQueryWrapper.eq(SysDepartment::getCompanyId, CurrentUserHolder.get().getCompanyId());
        departmentQueryWrapper.eq(SysDepartment::getParentId, 0);

        List<SysDepartment> departments = departmentMapper.selectList(departmentQueryWrapper);

        List<Long> idsList = departments.stream().map(department -> {
            return department.getId();
        }).collect(Collectors.toList());
        return idsList;
    }

    @Override
    public List<DepartmentOrUserSimpleDTO> querySimpleDepartment(Long departmentId, Integer includeMember) {
//      健壮性判断
        if(departmentId == null || includeMember == null){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }

        List<DepartmentOrUserSimpleDTO> list=new ArrayList<>();

//        获取部门列表
        LambdaQueryWrapper<SysDepartment> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysDepartment::getCompanyId,CurrentUserHolder.get().getCompanyUserId());
        lambdaQueryWrapper.eq(SysDepartment::getParentId,departmentId);
        List<SysDepartment> departments = departmentMapper.selectList(lambdaQueryWrapper);

        if (CollectionUtil.isNotEmpty(departments)){
            List<DepartmentOrUserSimpleDTO> dtos = departments.stream().map(department -> {
                DepartmentOrUserSimpleDTO dto = new DepartmentOrUserSimpleDTO();
                dto.setId(department.getId());
                dto.setName(department.getName());
                dto.setType(1);
                return dto;
            }).collect(Collectors.toList());
            list.addAll(dtos);
        }

//      获取员工列表接口
//        includeMember   0 为包含 ; 1 为 不包含
        if (includeMember==0){
            LambdaQueryWrapper<SysCompanyUser> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(SysCompanyUser::getCompanyId,CurrentUserHolder.get().getCompanyId());
            queryWrapper.eq(SysCompanyUser::getDepartmentId,departmentId);
            List<SysCompanyUser> sysCompanyUsers = companyUserMapper.selectList(queryWrapper);

            if (CollectionUtil.isNotEmpty(sysCompanyUsers)){
                List<DepartmentOrUserSimpleDTO> dtos = sysCompanyUsers.stream().map(sysCompanyUser -> {
                    DepartmentOrUserSimpleDTO userSimpleDTO = new DepartmentOrUserSimpleDTO();
                    userSimpleDTO.setId(sysCompanyUser.getId());
                    userSimpleDTO.setName(sysCompanyUser.getUserName());
                    userSimpleDTO.setType(2);

                    return userSimpleDTO;
                }).collect(Collectors.toList());
                list.addAll(dtos);
            }
        }
        return list;
    }

    @Override
    public List<Long> queryDepartmentsByUserId(Long companyUserId) {
        List<Long> ids=new ArrayList<>();

        LambdaQueryWrapper<SysCompanyUser> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysCompanyUser::getCompanyId,CurrentUserHolder.get().getCompanyId());
        queryWrapper.eq(SysCompanyUser::getId,companyUserId);

        SysCompanyUser companyUser = companyUserMapper.selectOne(queryWrapper);

        if (companyUser!=null){
            Long departmentId = companyUser.getDepartmentId();
            ids.add(departmentId);

            while (true){
                SysDepartment sysDepartment = departmentMapper.selectById(departmentId);
                Long parentId = sysDepartment.getParentId();

                if (parentId!=null&&parentId!=0){
                    ids.add(parentId);
                    departmentId=parentId;
                }else {
                    break;
                }
            }
        }
        return ids;
    }
}
